from django.db import models
from django.utils.timezone import now


class Gateway(models.Model):
    key = models.CharField(db_column="c_key", max_length=32, verbose_name=u"主键", primary_key=True, blank=False,
                           null=False)
    gateway_id = models.IntegerField(db_column='c_gateway_id', verbose_name="通讯ID", null=False)
    client_id = models.CharField(db_column="c_client_id", max_length=20, verbose_name="mqtt客户端ID", null=False)
    script_id = models.CharField(db_column="c_script_id", max_length=32, verbose_name=u"主键", blank=False, null=False)
    script_name = models.CharField(db_column="c_script_name", max_length=20, verbose_name="脚本库名称", null=False,
                                   blank=False)
    name = models.CharField(db_column="c_name", max_length=20, verbose_name="网关", blank=False, null=False)
    model_number = models.CharField(db_column="c_model_number", max_length=20, verbose_name="型号", blank=False,
                                    null=False)
    mcu_id = models.CharField(db_column="c_mcu_id", max_length=100, verbose_name='mac地址', null=False, blank=False)
    state = models.BooleanField(db_column="c_state", verbose_name="状态", blank=False, null=False, default=False)
    is_running = models.BooleanField(db_column="c_is_running", default=False)
    active_state = models.IntegerField(db_column="c_active_state", verbose_name="激活状态", default=-2)
    file_path = models.CharField(max_length=100, db_column="c_file_path", verbose_name="配置文件路径", default="")
    is_delete = models.BooleanField(db_column="c_is_delete", default=False)
    is_tac31 = models.BooleanField(db_column="c_is_tac31", default=False)
    create_date = models.DateTimeField(db_column="c_create_date", default=now)
    update_date = models.DateTimeField(db_column="c_update_date", default=now)
    last_log_in = models.DateTimeField(db_column="c_last_log_in", default=now)
    last_exit_out = models.DateTimeField(db_column="c_last_exit_out", default=now)
    singal = models.IntegerField(db_column="c_singal", default=0)
    sim_status = models.CharField(db_column="c_sim_status", max_length=100, verbose_name="sim卡状态")
    sim_number = models.CharField(db_column="c_sim_number", max_length=100, verbose_name="sim卡号")

    class Meta:
        db_table = "t_gateway"
        verbose_name = "网关表"
        verbose_name_plural = verbose_name
        ordering = ["-create_date"]


class Sensor(models.Model):
    id = models.CharField(db_column="c_id", max_length=32, verbose_name=u"主键", primary_key=True, blank=False,
                          null=False)
    gateway_id = models.CharField(db_column="c_gateway_id", max_length=32, verbose_name="网关")
    number = models.IntegerField(db_column="c_number", verbose_name="编号", null=False, blank=False)
    is_delete = models.BooleanField(db_column="c_is_delete", default=False)
    create_date = models.DateTimeField(db_column="c_create_date", default=now)
    update_date = models.DateTimeField(db_column="c_update_date", default=now)

    class Meta:
        db_table = "t_sensor"
        verbose_name = "传感器表"
        verbose_name_plural = verbose_name
        ordering = ["create_date"]

    @classmethod
    def delete_sensors(cls, key):
        Sensor.objects.filter(gateway_id=key).delete()
